package com.example.view;

import java.util.ArrayList;
import java.util.List;

import com.example.qzzm.R;
import com.example.window.WindowUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyHorizontalScrollView extends HorizontalScrollView implements
		OnClickListener {

	// 存储屏幕信息
	private int mScreenWidth;

	// 用LinearLayout存放内容
	private LinearLayout container;

	// Universal-image的参数设置
	private DisplayImageOptions options;

	private Context context;

	// 当前显示的图标
	private int currentIndex = 0;

	// 存储图片路径
	private List<String> urlsList;

	// 存储ImageView
	private List<Holder> holders = new ArrayList<MyHorizontalScrollView.Holder>();

	// 是否第一次绘制
	private boolean isFirstDraw = true;

	public MyHorizontalScrollView(Context context) {
		this(context, null);
	}

	public MyHorizontalScrollView(Context context, AttributeSet attrs,
			int defStyle) {
		this(context, attrs);
	}

	public MyHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		mScreenWidth = WindowUtil.getWidth();
	}

	private void checkShow(float x, float start2) {
		// TODO Auto-generated method stub

		// 向右移动
		if ((x - start2) > 0) {
			showPre();
		}
		if ((start2 - x) > 0) {
			showNext();
		} 
	}

	private void showPre() {
		System.out.println("pre");
		this.smoothScrollTo((--currentIndex) * mScreenWidth, 0);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);

	}

	public void showNext() {
		System.out.println("nex");
		this.smoothScrollTo((++currentIndex) * mScreenWidth, 0);
	}

	private void resetPosition() {
		System.out.println("res");
		this.smoothScrollTo(currentIndex * mScreenWidth, 0);
		invalidate();
	}

	
	
	float start;

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			start = ev.getX();
			break;
		case MotionEvent.ACTION_UP:
			checkShow(ev.getX(), start);
			return true;
		default:
			break;
		}

		return super.onTouchEvent(ev);
	}

	/**
	 * 
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		container = (LinearLayout) getChildAt(0);
		if (isFirstDraw) {
			init();
			isFirstDraw = false;
		}
	};

	/**
	 * 初始化界面
	 */
	public void init() {

		int i = 0;
		for (String url : urlsList) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(R.drawable.app_default);
			container.addView(imageView);
			prepareData(url, imageView);
			holders.add(new Holder(i++, url, imageView));
		}

	}

	/**
	 * 设置具体内容
	 * 
	 * @param views
	 */
	public void setContentUrls(List<String> urls) {
		urlsList = urls;
	}

	class Holder {
		ImageView imView;
		String url;
		int index;

		public Holder(int i, String u, ImageView iv) {
			this.imView = iv;
			this.url = u;
			this.index = i;
		}
	}

	/**
	 * 网络下载图片
	 * 
	 * @param url
	 * @param v
	 * @return
	 */
	public void prepareData(String url, ImageView v) {
		// 设定参数
		options = new DisplayImageOptions.Builder()
		// 设置图片在下载期间显示的图片
				.showStubImage(R.drawable.app_default)
				// 设置图片Uri为空或是错误的时候显示的图片
				.showImageForEmptyUri(R.drawable.main_header)
				// 设置图片加载/解码过程中错误时候显示的图片
				.showImageOnFail(R.drawable.main_header)
				// 设置下载的图片是否缓存在内存中
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在SD卡中
				.cacheOnDisc(true)
				// 设置图片以如何的编码方式显示
				.imageScaleType(ImageScaleType.NONE)
				// 设置为RGB565比起默认的ARGB_8888要节省大量的内存
				.bitmapConfig(Bitmap.Config.RGB_565)
				// 载入图片前稍做延时可以提高整体滑动的流畅度
				.delayBeforeLoading(100)
				// 显示图片时渐变
				.displayer(new FadeInBitmapDisplayer(500)).build();
		ImageLoader.getInstance().displayImage(url, v, options);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
